if (!require("plotly")) install.packages("plotly")
library(plotly)
if (!require("ggplot2")) install.packages("ggplot2")
library(ggplot2)
if (!require("psych")) install.packages("psych")
library(psych)
if (!require("rgl")) install.packages("rgl")
library(psych)
if (!require("RColorBrewer")) install.packages("RColorBrewer")
library(RColorBrewer)
if (!require("corrplot")) install.packages("corrplot")
library(corrplot)
if (!require("MASS")) install.packages("MASS")
library(MASS)
if (!require("plot3D")) install.packages("plot3D")
library(MASS)
if (!require("lattice")) install.packages("lattice")
library(lattice)
if (!require("reshape")) install.packages("reshape")
library(lattice)
head(iris)
head(Orange)
head(mpg)
?plot
plot(iris$Sepal.Length, iris$Sepal.Width, pch=21, main="Iron man", type = "b", xlab = "Xdirector")
plot(iris$Petal.Length, iris$Sepal.Width,
pch=21, bg=c("red","green","blue")[unclass(iris$Species)],
main="Second Scatter Plot - Iris Data")
qplot(iris$Sepal.Length, iris$Sepal.Width)
scatter <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width))
scatter + geom_point(aes(color=Species, shape=Species)) +
xlab("Sepal Length") + ylab("Sepal Width") +
ggtitle("Sepal Length-Width")
ggplot (iris, aes (x = Sepal.Length, y = Sepal.Width, colour = Species)) + stat_density2d ()
pal <- c("red", "blue", "green")
pal <- setNames(pal, c("virginica", "setosa", "versicolor"))
plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, colors = pal)
with(iris, plot_ly(iris, x = Sepal.Length, y= Petal.Length, z = Sepal.Width,
size = Petal.Width, color = Species,
type="scatter3d", mode="markers"))
if (!require("scatterplot3d")) install.packages("scatterplot3d")
library(scatterplot3d)
scatterplot3d(iris[,1:3], color=as.integer(iris$Species))
library(lattice)
xyplot(Sepal.Length ~ Sepal.Width, iris, groups = iris$Species, pch= 20)
xyplot(Sepal.Width ~ Sepal.Length | Species, iris, groups = iris$Species, pch= 20)
cloud(Sepal.Length ~ Petal.Length * Petal.Width, data = iris,
groups = Species, screen = list(z = 20, x = -70),
perspective = FALSE,
key = list(title = "Iris Data", x = .15, y=.85, corner = c(0,1),
border = TRUE,
points = Rows(trellis.par.get("superpose.symbol"), 1:3),
text = list(levels(iris$Species))))
pairs(iris[1:4], main = "Iris Data", pch = 21,
bg = c("red", "green3", "blue")[unclass(iris$Species)])
pairs(iris[1:4], main = "Iris Data", pch = 21,
bg = c("red", "green3", "blue")[unclass(iris$Species)],
lower.panel = NULL)
my_cols <- c("#00AFBB", "#E7B800", "#FC4E07")
pairs(iris[,1:4], pch = 19, cex = 0.5,
col = my_cols[iris$Species],
lower.panel=NULL)
library(psych)
pairs.panels(iris[,-5],
method = "pearson", # correlation method
hist.col = "#00AFBB",
density = TRUE, # show density plots
ellipses = TRUE # show correlation ellipses
)
facet <- ggplot(data=iris, aes(Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point(aes(shape=Species), size=1.5) + geom_smooth(method="lm") +
xlab("Sepal Length") + ylab("Sepal Width") + ggtitle("Faceting")
# Along rows
facet + facet_grid(. ~ Species)
facet <- ggplot(data=iris, aes(Sepal.Length, y=Sepal.Width, color=Species)) +
geom_point(aes(shape=Species), size=1.5) + geom_smooth(method="lm") +
xlab("Sepal Length") + ylab("Sepal Width") + ggtitle("Faceting")
# Along columns
facet + facet_grid(Species ~ .)
hist(iris$Petal.Length, freq=FALSE, col="green",
xlab="Petal Length", main="Colored histogram")
curve(dnorm(x, mean=mean(iris$Petal.Length),
sd=sd(iris$Petal.Length)), add=TRUE, col="red") #line
ggplot(iris, aes(x=Petal.Length, y=..density..)) +
geom_histogram(fill="cornsilk", binwidth=0.2, colour="grey60", size=.2) +
geom_density()
qplot(Sepal.Length, data=iris, geom="histogram", fill=Species, binwidth=0.2)
plot_ly(alpha = 0.6) %>%
add_histogram(x = ~iris$Petal.Length) %>%
add_histogram(x = ~iris$Sepal.Length) %>%
layout(barmode = "overlay")
data = c(179718,41370,41914,44280)
pct = (data/sum(data))*100
pct = round(pct,2)
labels = c("Army", "Navy", "Air Force","Marines")
labels = paste(labels,pct, "%")
col = c("purple", "violetred1", "green3","red", "cyan")
pie(pct,col = col, radius = 1, init.angle = 90, clockwise = TRUE,
labels =labels, main = "Traumatic Brain Injury 2000-2014(Q2)")
library(RColorBrewer)
barplot(table(iris$Species,iris$Sepal.Length),col = brewer.pal(3,"Set1")) #Stacked Plot
library(reshape2)
iris2 <- melt(iris, id.vars="Species")
iris2[1:3,]
bar1 <- ggplot(data=iris2, aes(x=Species, y=value, fill=variable))
bar1 + geom_bar(stat="identity", position="dodge") +
scale_fill_manual(values=c("orange", "blue", "darkgreen", "purple"),
name="Iris\nMeasurements",
breaks=c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"),
labels=c("Sepal Length", "Sepal Width", "Petal Length", "Petal Width"))
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, type = 'bar', name = 'Sepal.Width') %>%
add_trace(y = ~Petal.Length, name = 'Petal.Length') %>%
add_trace(y = ~Petal.Width, name = 'Petal.Width')
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, type = 'bar', name = 'Sepal.Width') %>%
add_trace(y = ~Petal.Length, name = 'Petal.Length') %>%
add_trace(y = ~Petal.Width, name = 'Petal.Width') %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
ind1=which(Orange$Tree=="1")
ind2=which(Orange$Tree=="2")
ind3=which(Orange$Tree=="3")
ind4=which(Orange$Tree=="4")
ind5=which(Orange$Tree=="5")
plot(Orange$age[ind1],Orange$circumference[ind1],main="Orange tree growth",
xlab="Age",ylab="Circumference",type="l",cex.lab=1.25,cex.axis=0.8,xlim=c(100,1600),
ylim=c(25,215))
lines(Orange$age[ind2],Orange$circumference[ind2],col=2)
lines(Orange$age[ind3],Orange$circumference[ind3],col=3)
lines(Orange$age[ind4],Orange$circumference[ind4],col=4)
lines(Orange$age[ind5],Orange$circumference[ind5],col=5,lty=2)
legend(100,180,legend=c("Tree 1","Tree 2","Tree 3","Tree 4","Tree 5"),col=1:5,lty=c(rep(1,4),2))
qplot(age, circumference, data = Orange, geom = c("point", "line"), color = Tree)
ind1=which(Orange$Tree=="1")
ind2=which(Orange$Tree=="2")
ind3=which(Orange$Tree=="3")
ind4=which(Orange$Tree=="4")
ind5=which(Orange$Tree=="5")
p<- plot_ly(Orange, x = ~age[ind1], y = ~circumference[ind1], name = 'Tree 1', type = 'scatter',
mode = 'lines') %>%
add_trace(y = ~circumference[ind2], name = 'Tree 2', mode = 'lines+markers') %>%
add_trace(y = ~circumference[ind3], name = 'Tree 3', mode = 'lines+markers') %>%
add_trace(y = ~circumference[ind4], name = 'Tree 4', mode = 'lines+markers') %>%
add_trace(y = ~circumference[ind5], name = 'Tree 5', mode = 'lines+markers')
p
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
nba$Name <- with(nba, reorder(Name, PTS))
# scaling <- function(x) { (x - min(x))/(max(x) - min(x))}
normed <- as.data.frame(lapply(nba[,-1], rescale))
normed$Name <- nba$Name
nba.m <- melt(normed)
head(nba)
ggplot(nba.m, aes(variable, Name)) +
geom_tile(aes(fill = value), colour = "#D3E3F3") +
scale_fill_gradient(low = "#FFFFFF", high = "#08306B") +
scale_x_discrete("", expand = c(0, 0)) +
scale_y_discrete("", expand = c(0, 0)) +
theme_grey(base_size = 9) +
theme(legend.position = "none",
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 330, hjust = 0))
NOTE: This part runs well in R Studio, but gives an error in Jupyter: feel free to try it out there.
nba.m$Category <- nba.m$variable
levels(nba.m$Category) <-
list("Offensive" = c("PTS", "FGM", "FGA", "AST", "X3PM", "X3PA"),
"Defensive" = c("DRB", "ORB", "TRB","BLK", "PF", "STL"),
"Other" = c("G", "MIN", "FGP", "FTM", "FTA", "FTP", "X3PP", "TO"))
nba.m$rescaleoffset <- nba.m$value + 100*(as.numeric(nba.m$Category)-1)
scalerange <- range(nba.m$value)
gradientends <- scalerange + rep(c(0,100,200), each=2)
colorends <- c("white", "red", "white", "green", "white", "blue")
# ggplot(nba.m, aes(variable, Name)) +
# geom_tile(aes(fill = rescaleoffset), colour = "white") +
# scale_fill_gradientn(colours = colorends, values = rescale(gradientends)) +
# scale_x_discrete("", expand = c(0, 0)) +
# scale_y_discrete("", expand = c(0, 0)) +
# theme_grey(base_size = 9) +
# theme(legend.position = "none",
# axis.ticks = element_blank(),
# axis.text.x = element_text(angle = 330, hjust = 0))
NOTE: This part runs well in R Studio, but gives an error in Jupyter: feel free to try it out there.
nba.m$variable2 <- reorder(nba.m$variable, as.numeric(nba.m$Category))
# ggplot(nba.m, aes(variable2, Name)) +
# geom_tile(aes(fill = rescaleoffset), colour = "white") +
# scale_fill_gradientn(colours = colorends, values = rescale(gradientends)) +
# scale_x_discrete("", expand = c(0, 0)) +
# scale_y_discrete("", expand = c(0, 0)) +
# theme_grey(base_size = 9) +
# theme(legend.position = "none",
# axis.ticks = element_blank(),
# axis.text.x = element_text(angle = 330, hjust = 0))